package com.hgyc.mom.system.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.system.entity.SysMenu;
import com.hgyc.mom.system.service.SysMenuService;
import com.hgyc.mom.system.vo.MenuVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统菜单 前端控制器
 * </p>
 *
 * @author fwj
 * @since 2025-01-16
 */
@RestController
@RequestMapping("/sys/menu")
@Tag(name = "系统菜单接口", description = "系统菜单接口")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public Result<List<MenuVO>> getAll() {
        List<MenuVO> roleMenus = sysMenuService.all();
        return Result.success(roleMenus);
    }

    @RequestMapping(value = "/roleAuth/{roleId}", method = RequestMethod.GET)
    public Result<List<Long>> roleAuth(@PathVariable("roleId") Long roleId) {
        List<Long> roleMenus = sysMenuService.roleAuth(roleId);
        return Result.success(roleMenus);
    }

    @RequestMapping(value = "/userAuth", method = RequestMethod.GET)
    public Result<List<MenuVO>> queryUserInfo() {
        List<MenuVO> roleMenus = sysMenuService.getUserMenu((Long) StpUtil.getLoginId());
        return Result.success(roleMenus);
    }

    @RequestMapping(value = "/queryById/{id}", method = RequestMethod.GET)
    public Result<SysMenu> queryById(@PathVariable("id") Long id) {
        SysMenu sysMenu = sysMenuService.queryById(id);
        return Result.success(sysMenu);
    }

    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    public Result<?> saveOrUpdate(@RequestBody SysMenu sysMenu) {
        if (sysMenu.getParentId() == null) {
            sysMenu.setParentId(0L);
        }
        sysMenuService.saveOrUpdate(sysMenu);
        return Result.success();
    }

    @RequestMapping(value = "/del/{id}", method = RequestMethod.DELETE)
    public Result<?> deleteMenu(@PathVariable("id") Long id) {
        sysMenuService.deleteMenu(id);
        return Result.success("删除成功");
    }
}
